﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Configuration;
using Ivy.Common.Framework.GeneralUtility;
using Ivy.Common.Framework.Configuration;

namespace Ivy.Common.Framework.EmailUtility
{
    public enum EmailAddressType
    {
        SendTo = 0,
        SendCC = 1,
        SendBcc = 2,
        ReplyTo = 3
    }

    public class EmailUtility
    {
        #region Variables & Constants
        MailMessage _MailMessage;
        const string _FrameworkIvyConfigSection = "FrameworkIvyConfigSection";
        const string _DefaultSmtpServer = "DefaultSmtpServer";
        #endregion Variables & Constants

        #region Constructor & Destructor
        #endregion Constructor & Destructor

        #region Public Properties
        #endregion Public Properties

        #region Public Methods
        public SmtpClient GetSmptClient()
        {
            SmtpClient lSmtpClient = new SmtpClient();
            AppSettingsReader _AppSettingsReader = new AppSettingsReader();
            string _DefaultSmtpServerName = _AppSettingsReader.GetValue(_DefaultSmtpServer, typeof(string)).ToString();
            SmtpServer _SmtpServer = (((FrameworkIvyConfigSection)ConfigurationManager.GetSection(_FrameworkIvyConfigSection)).SmtpMailServers[_DefaultSmtpServerName]);

            if (!string.IsNullOrEmpty(_SmtpServer.SmtpHost))
            {
                if (!string.IsNullOrEmpty(_SmtpServer.SmtpPort))
                {
                    lSmtpClient.Host = _SmtpServer.SmtpHost;
                    lSmtpClient.Port = Int32.Parse(_SmtpServer.SmtpPort);
                    lSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                    if (!string.IsNullOrEmpty(_SmtpServer.SmtpAuthUserId))
                    {
                        if (!string.IsNullOrEmpty(_SmtpServer.SmtpAuthUserPassword))
                        {
                            if (!string.IsNullOrEmpty(_SmtpServer.IsHostSSLEnabled))
                            {
                                if (!string.IsNullOrEmpty(_SmtpServer.IsPasswordEncrypted))
                                {
                                    if (bool.Parse(_SmtpServer.IsPasswordEncrypted))
                                    {
                                        lSmtpClient.Credentials = new System.Net.NetworkCredential(_SmtpServer.SmtpAuthUserId, StringUtility.DecryptString(_SmtpServer.SmtpAuthUserPassword));
                                    }
                                    else
                                    {
                                        lSmtpClient.Credentials = new System.Net.NetworkCredential(_SmtpServer.SmtpAuthUserId, _SmtpServer.SmtpAuthUserPassword);
                                    }
                                }
                                else
                                {
                                    {
                                        lSmtpClient.Credentials = new System.Net.NetworkCredential(_SmtpServer.SmtpAuthUserId, _SmtpServer.SmtpAuthUserPassword);
                                    }
                                }
                                lSmtpClient.EnableSsl = bool.Parse(_SmtpServer.IsHostSSLEnabled);
                            }
                        }
                    }
                }
            }
            return lSmtpClient;
        }

        public SmtpClient GetSmptClient(string AppSettingsConfigKeyForSmtpServer)
        {
            SmtpClient lSmtpClient = new SmtpClient();
            SmtpServer _SmtpServer = (((FrameworkIvyConfigSection)ConfigurationManager.GetSection(_FrameworkIvyConfigSection)).SmtpMailServers[AppSettingsConfigKeyForSmtpServer]);

            if (!string.IsNullOrEmpty(_SmtpServer.SmtpHost))
            {
                if (!string.IsNullOrEmpty(_SmtpServer.SmtpPort))
                {
                    lSmtpClient.Host = _SmtpServer.SmtpHost;
                    lSmtpClient.Port = Int32.Parse(_SmtpServer.SmtpPort);
                    lSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                    if (!string.IsNullOrEmpty(_SmtpServer.SmtpAuthUserId))
                    {
                        if (!string.IsNullOrEmpty(_SmtpServer.SmtpAuthUserPassword))
                        {
                            if (!string.IsNullOrEmpty(_SmtpServer.IsHostSSLEnabled))
                            {
                                if (!string.IsNullOrEmpty(_SmtpServer.IsPasswordEncrypted))
                                {
                                    if (bool.Parse(_SmtpServer.IsPasswordEncrypted))
                                    {
                                        lSmtpClient.Credentials = new System.Net.NetworkCredential(_SmtpServer.SmtpAuthUserId, StringUtility.DecryptString(_SmtpServer.SmtpAuthUserPassword));
                                    }
                                    else
                                    {
                                        lSmtpClient.Credentials = new System.Net.NetworkCredential(_SmtpServer.SmtpAuthUserId, _SmtpServer.SmtpAuthUserPassword);
                                    }
                                }
                                else
                                {
                                    {
                                        lSmtpClient.Credentials = new System.Net.NetworkCredential(_SmtpServer.SmtpAuthUserId, _SmtpServer.SmtpAuthUserPassword);
                                    }
                                }
                                lSmtpClient.EnableSsl = bool.Parse(_SmtpServer.IsHostSSLEnabled);
                            }
                        }
                    }
                }
            }
            return lSmtpClient;
        }
        public SmtpClient GetSmptClient(string smtpHost, string smtpPort, string smtpUserId, string smtpUserPassword, bool isPasswordEncrypted, bool isSSLEnabled)
        {
            SmtpClient lSmtpClient = new SmtpClient();

            if (string.IsNullOrEmpty(smtpHost))
            {
                if (string.IsNullOrEmpty(smtpPort))
                {
                    if (string.IsNullOrEmpty(smtpUserId))
                    {
                        if (string.IsNullOrEmpty(smtpUserPassword))
                        {
                            lSmtpClient.Host = smtpHost;
                            lSmtpClient.Port = Int32.Parse(smtpPort);
                            if (isPasswordEncrypted)
                            {
                                lSmtpClient.Credentials = new System.Net.NetworkCredential(smtpUserId, StringUtility.DecryptString(smtpUserPassword));
                            }
                            else
                            {
                                lSmtpClient.Credentials = new System.Net.NetworkCredential(smtpUserId, smtpUserPassword);
                            }
                            lSmtpClient.EnableSsl = isSSLEnabled;
                        }
                    }
                }
            }
            return lSmtpClient;
        }
        public void AddEmailSenderAddressToMailMessage(MailMessage mailMessageObject, string emailAddress)
        {
            if (!string.IsNullOrEmpty(emailAddress))
            {
                mailMessageObject.From = new MailAddress(emailAddress);
            }
        }

        public void AddEmailSenderAddressToMailMessage(MailMessage mailMessageObject, string emailAddress, string emailDisplayName)
        {
            if (!string.IsNullOrEmpty(emailAddress))
            {
                mailMessageObject.From = new MailAddress(emailAddress, emailDisplayName);
            }
        }
        public void AddEmailRecepientOrReplyAddressesToMailMessage(MailMessage mailMessageObject, EmailAddressType emailAddressType, string semicolonSeperatedEmailAddresses)
        {
            string[] lEmailAddresses = semicolonSeperatedEmailAddresses.Split(';');
            foreach (string lEmailAddress in lEmailAddresses)
            {
                if (!string.IsNullOrEmpty(lEmailAddress))
                {
                    if (emailAddressType == EmailAddressType.SendTo)
                    {
                        mailMessageObject.To.Add(lEmailAddress);
                    }
                    if (emailAddressType == EmailAddressType.SendCC)
                    {
                        mailMessageObject.CC.Add(lEmailAddress);
                    }
                    if (emailAddressType == EmailAddressType.SendBcc)
                    {
                        mailMessageObject.Bcc.Add(lEmailAddress);
                    }
                }
            }
        }
        public void SendEmail(MailMessage mailMessageObject)
        {
            SendEmail(GetSmptClient(), mailMessageObject);
        }
        public void SendEmail(string AppSettingsConfigKeyForSmtpServer, MailMessage mailMessageObject)
        {
            SendEmail(GetSmptClient(AppSettingsConfigKeyForSmtpServer), mailMessageObject);
        }
        public void SendEmail(SmtpClient smtpClientObject, MailMessage mailMessageObject)
        {
            if (mailMessageObject.From != null)
            {
                if (!(mailMessageObject.To == null && mailMessageObject.CC == null && mailMessageObject.Bcc == null))
                {
                    smtpClientObject.Send(mailMessageObject);
                }
            }
        }
        #endregion Public Methods

        #region Events
        #endregion Events

        #region Private Methods
        #endregion Private Methods
    }
}
